<!--
 * @Author: daidai
 * @Date: 2022-01-12 14:23:32
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-09-09 14:47:24
 * @FilePath: \web-pc\src\pages\big-screen\view\home.vue
-->
<template>
  <!-- <div id="index" ref="appRef" class="index_home" :class="{ pageisScale: isScale }"> -->
  <ScaleScreen
    :width="1920"
    :height="1080"
    class="scale-wrap"
    :selfAdaption="$store.state['xing-bian-screen-setting'].isScale"
  >
    <div class="bg">
      <dv-loading v-if="loading">Loading...</dv-loading>
      <div v-else class="host-body">
        <!-- 头部 s -->
        <div class="d-flex jc-center title_wrap">
          <div class="guang"></div>
          <div class="timers">
            <button
              class="back-button"
              @click="goBack"
              title="返回"
            >
              <img src="@/assets/xing-bian-screen/img/back.svg" alt="返回" class="back-icon" />
              返回
            </button>
            {{ dateYear }} {{ dateWeek }} {{ dateDay }}
            <i
              class="blq-icon-shezhi02"
              style="margin-left: 10px"
              @click="showSetting"
            />
          </div>
          <div class="d-flex jc-center">
            <div class="title">
              <span class="title-text">形变监测预警平台</span>
            </div>
          </div>
          <div class="fullscreen-container">
            <button
              class="fullscreen-button"
              @click="toggleFullscreen"
              :title="isFullscreen ? '退出全屏' : '全屏'"
            >
              <img 
                v-show="!isFullscreen"
                src="@/assets/xing-bian-screen/img/full-screen.svg" 
                alt="全屏" 
                class="fullscreen-icon" 
              />
              {{ isFullscreen ? '退出全屏' : '全屏' }}
            </button>
          </div>
        </div>
        <!-- 头部 e-->
        <!-- 内容  s-->
        <router-view></router-view>
        <!-- 内容 e -->
      </div>
    </div>
    <Setting ref="setting" />
  </ScaleScreen>
  <!-- </div> -->
</template>

<script>
import { formatTime } from "@/utils/xing-bian-screen/index.js";
import Setting from "./setting.vue";
import ScaleScreen from "@/components/xing-bian-screen/scale-screen/scale-screen.vue";
import tokenManager from "@/utils/tokenManager";
export default {
  components: { Setting, ScaleScreen },
  data() {
    return {
      timing: null,
      loading: true,
      dateDay: null,
      dateYear: null,
      dateWeek: null,
      weekday: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
      isFullscreen: false,
    };
  },
  filters: {
    numsFilter(msg) {
      return msg || 0;
    },
  },
  computed: {},
  created() {},
  mounted() {
    this.timeFn();
    this.cancelLoading();
    // 启动token自动刷新机制
    tokenManager.startAutoRefresh();
    
    // 监听全屏状态变化
    document.addEventListener('fullscreenchange', this.handleFullscreenChange);
    document.addEventListener('webkitfullscreenchange', this.handleFullscreenChange);
    document.addEventListener('mozfullscreenchange', this.handleFullscreenChange);
    document.addEventListener('MSFullscreenChange', this.handleFullscreenChange);
  },
  beforeDestroy() {
    clearInterval(this.timing);
    // 停止token自动刷新
    tokenManager.stopAutoRefresh();
    
    // 移除全屏状态变化的事件监听器
    document.removeEventListener('fullscreenchange', this.handleFullscreenChange);
    document.removeEventListener('webkitfullscreenchange', this.handleFullscreenChange);
    document.removeEventListener('mozfullscreenchange', this.handleFullscreenChange);
    document.removeEventListener('MSFullscreenChange', this.handleFullscreenChange);
  },
  methods: {
    showSetting() {
      this.$refs.setting.init();
    },
    goBack() {
      console.log('goBack method called');
      console.log('Current route:', this.$route);
      console.log('Router instance:', this.$router);
      
      try {
        this.$router.push('/dashboard');
        console.log('Navigation to /dashboard successful');
      } catch (error) {
        console.error('Router navigation failed:', error);
        // 使用浏览器原生后退作为备选方案
        window.history.back();
      }
    },
    handleFullscreenChange() {
      // 检测当前全屏状态
      this.isFullscreen = !!(
        document.fullscreenElement ||
        document.webkitFullscreenElement ||
        document.mozFullScreenElement ||
        document.msFullscreenElement
      );
    },
    toggleFullscreen() {
      const element = document.documentElement;
      
      if (!document.fullscreenElement) {
        // 进入全屏
        if (element.requestFullscreen) {
          element.requestFullscreen();
        } else if (element.mozRequestFullScreen) {
          element.mozRequestFullScreen();
        } else if (element.webkitRequestFullscreen) {
          element.webkitRequestFullscreen();
        } else if (element.msRequestFullscreen) {
          element.msRequestFullscreen();
        }
      } else {
        // 退出全屏
        if (document.exitFullscreen) {
          document.exitFullscreen();
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen();
        } else if (document.webkitExitFullscreen) {
          document.webkitExitFullscreen();
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen();
        }
      }
      // 状态更新现在由handleFullscreenChange方法处理
    },
    timeFn() {
      this.timing = setInterval(() => {
        this.dateDay = formatTime(new Date(), "HH: mm: ss");
        this.dateYear = formatTime(new Date(), "yyyy-MM-dd");
        this.dateWeek = this.weekday[new Date().getDay()];
      }, 1000);
    },
    cancelLoading() {
      let timer = setTimeout(() => {
        this.loading = false;
        clearTimeout(timer);
      }, 500);
    },
  },
};
</script>

<style lang="scss">
@import "./home.scss";
</style>
